﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using CUWo.Common;
using Microsoft.Phone.Controls;

/**
 * 创建时间：2014/11/7 16:17:57
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.App.Views
{


    public sealed class WebViewPage
    {
        //public event EventHandler OnCloseIconHandler, OnOpenAdContentScreenHandler;

        private Popup popup;
        private Grid gridMain;
        private WebBrowser webViewMain;
        private StackPanel spProgressInfo;
        private string navigateUrl = string.Empty;


        public WebViewPage()
        {
            if (popup == null)
            {
                popup = new Popup();
            }
            popup.Height = CUWo.Common.DeviceInfoHelper.GetDeviceScreenHeight;// -26;//无通知栏
            popup.Width = DeviceInfoHelper.GetDeviceScreenWidth;
            this.popup.VerticalOffset = 0;//26;
            this.popup.HorizontalOffset = 0;
        }

        public WebViewPage(string navigateUrl)
            : this()
        {
            this.navigateUrl = navigateUrl;
            //
            this.gridMain = new Grid();
            this.webViewMain = new WebBrowser();
            this.webViewMain.IsScriptEnabled = true;
            this.webViewMain.Height = this.popup.Height;
            this.webViewMain.Width = this.popup.Width;
            //
            this.webViewMain.Navigating += webViewMain_Navigating;
            this.webViewMain.Navigated += webViewMain_Navigated;
            this.webViewMain.NavigationFailed += webViewMain_NavigationFailed;
            this.webViewMain.LoadCompleted += webViewMain_LoadCompleted;
            //
            //进度提示栏
            this.DrawProgressInfoToWebViewPage();
            //
            this.gridMain.Children.Add(this.webViewMain);
            this.gridMain.Children.Add(this.spProgressInfo);
            //
            this.OpenWebViewPage();
        }

        void webViewMain_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            LogHelper.LogInfo("webViewMain_LoadCompleted", this.GetType(), MethodBase.GetCurrentMethod());
            //这个事件会等到整个加载完成才执行
            //this.spProgressInfo.Visibility = Visibility.Collapsed;
        }

        void webViewMain_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
        {
            LogHelper.LogInfo("webViewMain_NavigationFailed", this.GetType(), MethodBase.GetCurrentMethod());
            foreach (UIElement element in this.spProgressInfo.Children)
            {
                if (element is TextBlock)
                {
                    (element as TextBlock).Text = "加载失败！";
                }
            }
        }

        void webViewMain_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            LogHelper.LogInfo("webViewMain_Navigated", this.GetType(), MethodBase.GetCurrentMethod());
            this.spProgressInfo.Visibility = Visibility.Collapsed;
        }

        void webViewMain_Navigating(object sender, NavigatingEventArgs e)
        {
            LogHelper.LogInfo("webViewMain_Navigating", this.GetType(), MethodBase.GetCurrentMethod());
        }


        /// <summary>
        /// 绘制打开目标地址的进度信息
        /// </summary>
        void DrawProgressInfoToWebViewPage()
        {
            this.spProgressInfo = new StackPanel() { Height = 80, Width = this.popup.Width, Background = new SolidColorBrush(Colors.Transparent), Orientation = Orientation.Vertical };
            this.spProgressInfo.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            this.spProgressInfo.HorizontalAlignment = HorizontalAlignment.Center;
            ProgressBar progressBar = new ProgressBar() { Height = 30, Width = this.popup.Width, IsIndeterminate = true, Background = new SolidColorBrush(Colors.Transparent), Visibility = Visibility.Visible };
            progressBar.VerticalAlignment = VerticalAlignment.Center;
            progressBar.HorizontalAlignment = HorizontalAlignment.Center;
            TextBlock txtInfo = new TextBlock() { Text = "正在加载中", FontSize = 16, HorizontalAlignment = HorizontalAlignment.Center };
            this.spProgressInfo.Children.Add(progressBar);
            this.spProgressInfo.Children.Add(txtInfo);
        }

        /// <summary>
        /// 必须先保证Popup正常显示
        /// </summary>
        private void OpenWebViewPage()
        {
            try
            {
                if (this.popup != null)
                {
                    this.popup.Child = this.gridMain;
                    if (string.IsNullOrWhiteSpace(this.navigateUrl))
                    {
                        this.CloseWebViewPage();
                        return;
                    }
                    this.popup.IsOpen = true;
                    this.webViewMain.Navigate(new Uri(this.navigateUrl));
                    //
                    //应用商店                    
                    //this.webViewMain.Navigate(new Uri("http://www.windowsphone.com/zh-cn/store/app/iq-mission/31701cfb-1d1a-487c-bf21-bd8e7a715ebb"));//args.WebErrorStatus=RedirectFailed                
                    //this.webViewMain.Navigate(new Uri("http://www.baidu.com"));
                    //this.webViewMain.Navigate(new Uri("http://www.mediabris.com"));
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            }
        }


        public void CloseWebViewPage()
        {
            if (this.popup != null)
            {
                try
                {
                    this.popup.IsOpen = false;
                    this.popup.Child = null;
                    //if (this.OnCloseIconHandler != null)
                    //{
                    //    //在此事件中必须使用-=来移除OnCloseIconHandler事件，否则会有死循环
                    //    this.OnCloseIconHandler(null, new EventArgs());
                    //}
                }
                catch (Exception err)
                {
                    LogHelper.LogError(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
                }
            }
        }
    }
}
